<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="单位名称" prop="unitName">
        <el-input
          v-model="queryParams.unitName"
          placeholder="请输入单位名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位类型" prop="unitType">
        <el-select
          v-model="queryParams.unitType"
          placeholder="请选择单位类型"
          clearable
          size="small"
          @change="handleQuery"
        >
          <el-option
            v-for="type in unitTypeOptions"
            :key="type.unittypeId"
            :label="type.unittypeName"
            :value="type.unittypeId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="查询状态" prop="searchDoingStatus">
        <el-select
          v-model="queryParams.searchDoingStatus"
          placeholder="请选择状态"
          clearable
          size="small"
          @change="handleStatus"
        >
          <el-option
            v-for="item in searchDoingStatus"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="unitList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="序号" align="left" prop="unitSort" width="100" />
      <el-table-column
        label="单位名称"
        align="left"
        prop="unitName"
        width="320"
      />
      <el-table-column label="联系人" align="left" prop="unitLinkman" />
      <el-table-column label="联系电话" align="left" prop="unitPhone" />
      <el-table-column label="法人" align="left" prop="unitRepresentative" />
      <!-- <el-table-column label="法人电话" align="left" prop="unitRepresentativePhone" /> -->
      <el-table-column label="单位类型" align="left" prop="unitBank" />
      <!-- <el-table-column label="单位状态" align="center" key="forbidden">
        <template slot-scope="scope">
          {{scope.row.forbidden == 0 ? "启用" : "已禁用"}}
        </template>
      </el-table-column> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-plus-outline"
            @click="handleDetail(scope.row)"
            >单位详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改单位信息对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1200px"
      margin-left="10px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="单位名称" prop="unitName">
              <el-input v-model="form.unitName" placeholder="请输入单位名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="组织机构代码">
              <el-input
                v-model="form.unitInstitutionId"
                placeholder="请输入组织机构代码"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="法定代表人" prop="unitRepresentative">
              <el-input
                v-model="form.unitRepresentative"
                placeholder="请输入法定代表人"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="法定代表人联系电话"
              prop="unitRepresentativePhone"
            >
              <el-input
                v-model="form.unitRepresentativePhone"
                placeholder="法定代表人联系电话"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="联系人" prop="unitLinkman">
              <el-input v-model="form.unitLinkman" placeholder="请输入联系人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="unitPhone">
              <el-input v-model="form.unitPhone" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="单位类型" prop="unitType">
              <el-select
                v-model="form.unitType"
                placeholder="请选择单位类型"
                style="width: 422.5px"
              >
                <el-option
                  v-for="type in unitTypeOptions"
                  :key="type.unittypeId"
                  :label="type.unittypeName"
                  :value="type.unittypeId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位序号" prop="unitSort">
              <el-input v-model="form.unitSort" placeholder="请输入单位序号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="资质等级">
              <el-input
                v-model="form.unitNaturalGrade"
                placeholder="请输入资质等级"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="注册地址" prop="unitAddress">
              <el-input
                v-model="form.unitAddress"
                placeholder="请输入注册地址"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="单位邮箱" prop="unitEmail">
              <el-input v-model="form.unitEmail" placeholder="请输入单位邮箱" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="可中签次数" prop="unitNum">
              <el-input
                v-model="form.unitNum"
                min="1"
                type="number"
                placeholder="请输入每月可中签次数"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="备注" prop="unitRemarks">
              <el-input v-model="form.unitRemarks" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col>
            <el-form-item label="资质范围">
              <el-input
                v-model="form.unitNaturalScope"
                placeholder="请输入资质范围"
                type="textarea"
                rows="4"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="是否注册在高新区:">
              <el-radio-group v-model="form.whetherRegister">
                <el-radio label="0">是</el-radio>
                <el-radio label="1">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否税收在高新区:">
              <el-radio-group v-model="form.whetherRevenue">
                <el-radio label="0">是</el-radio>
                <el-radio label="1">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 单位详情 -->
    <DetailDialog :visible.sync="unitopen" :formUnit="formUnit" :doNotDisplay="false"/>

    <Dialog
      :form="form"
      :items="items"
      :resultOpen.sync="resultOpen"
      :rowspanList="rowspanList"
    />
  </div>
</template>

<script>
import {
  listUnit,
  getUnit,
  delUnit,
  addUnit,
  updateUnit,
  exportUnit,
  modifyDisable,
  getUnitInfoList,
  getGoingUnit,
} from "@/api/unit/unit";
import { getExtract } from "@/api/unit/extract";
import { listUnittype } from "@/api/unit/unittype";
import { getRandomresult } from "@/api/unit/random";
import unitDetail from "@/common/unitDetail/unitDetail";
import Dialog from "./components/Dialog";
import DetailDialog from "@/common/detailDialog";

export default {
  name: "Unit",
  components: { unitDetail, Dialog, DetailDialog },
  data() {
    return {
      // 遮罩层
      loading: true,
      //默认选中
      label: 0,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 单位信息表格数据
      unitList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 单位类型字典
      unitTypeOptions: [],
      searchDoingStatus: [
        {
          label: "全部",
          value: 0,
        },
        {
          label: "有进行中的项目",
          value: 1,
        },
      ],
      // 查询参数
      queryParams: {
        forbidden: null,
        pageNum: 1,
        pageSize: 10,
        unitName: null,
        unitRepresentative: null,
        unitRepresentativePhone: null,
        unitInstitutionId: null,
        unitLinkman: null,
        unitPhone: null,
        unitType: null,
        unitEmail: null,
        unitBank: null,
        unitSort: null,
        unitNaturalGrade: null,
        unitNaturalScope: null,
        unitAddress: null,
        unitType: null,
        unitRemarks: null,
        unitStatues: null,
        unitNum: null,
        searchDoingStatus: 0,
      },
      unitTypeParams: {
        pageNum: 1,
        pageSize: 10,
        unittypeName: null,
      },
      modifyqueryParams: {
        forbidden: null,
        unitId: null,
      },
      // 表单参数
      form: {
        whetherRegister: "0",
        whetherRevenue: "0",
      },
      // 表单校验
      rules: {
        unitName: [
          { required: true, message: "单位名称不能为空", trigger: "blur" },
        ],
        unitType: [
          { required: true, message: "单位类型不能为空", trigger: "change" },
        ],
        unitRepresentative: [
          { required: true, message: "法定代表人不能为空", trigger: "blur" },
        ],
        unitRepresentativePhone: [
          {
            required: true,
            message: "法定代表人联系电话不能为空",
            trigger: "blur",
          },
        ],
        unitLinkman: [
          { required: true, message: "联系人不能为空", trigger: "blur" },
        ],
        unitPhone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" },
        ],
        unitSort: [
          { required: true, message: "单位序号不能为空", trigger: "blur" },
        ],
      },
      unitopen: false,
      formUnit: {},
      unitDetailRow: {},
      onGoingProject: [],
      form: [],
      items: [],
      resultOpen: false,
      rowspanList: 5,
    };
  },
  created() {
    this.getList();
    listUnittype().then((response) => {
      this.unitTypeOptions = response.rows;
    });
  },
  methods: {
    /** 查询单位信息列表 */
    getList() {
      this.loading = true;
      getUnitInfoList(this.queryParams)
        .then((response) => {
          this.unitList = response.rows;
          this.total = response.total;
        })
        .finally(() => (this.loading = false));
    },
    // 单位库类型翻译
    unitTypeFormat(row, column) {
      return this.selecttypeLabel(this.unitTypeOptions, row.unitType);
    },
    approveStatusFormat(row, column) {
      if (row.unitType == 1) {
        return "招标代理";
      } else if (row.unitType == 2) {
        return "施工图审查（含勘察报告审查）";
      } else if (row.unitType == 3) {
        return "预结算（含施工图预算清单编制、工程变更审核、结算内部）";
      } else if (row.unitType == 4) {
        return "工程财务决算";
      } else if (row.unitType == 5) {
        return "工程设计";
      } else if (row.unitType == 6) {
        return "工程勘察（含各阶段勘察工作、勘察外业见证）";
      } else if (row.unitType == 7) {
        return "工程监理";
      } else if (row.unitType == 8) {
        return "工程检测单位";
      } else if (row.unitType == 9) {
        return "预结算（跟踪审计）";
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    //转换单位库类型
    selecttypeLabel(datas, value) {
      var actions = [];
      Object.keys(datas).some((key) => {
        if (datas[key].unittypeId == "" + value) {
          actions.push(datas[key].unittypeName);
          return true;
        }
      });
      return actions.join("");
    },
    // 表单重置
    reset() {
      this.form = {
        unitId: null,
        unitName: null,
        unitRepresentative: null,
        unitInstitutionId: null,
        unitLinkman: null,
        unitPhone: null,
        unitType: null,
        unitEmail: null,
        unitBank: null,
        unitSort: null,
        unitNaturalGrade: null,
        unitNaturalScope: null,
        unitAddress: null,
        unitRemarks: null,
        unitStatues: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        unitNum: 1,
        forbidden: "0",
        whetherRegister: "0",
        whetherRevenue: "0",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.unitId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加单位信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const unitId = row.unitId || this.ids;
      getUnit(unitId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改单位信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.unitId != null) {
            updateUnit(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUnit(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 单位状态修改
    handleStatusChange(row) {
      let text = row.forbidden === "0" ? "启用" : "禁用";
      this.modifyqueryParams.forbidden = row.forbidden;
      this.modifyqueryParams.unitId = row.unitId;
      this.$confirm("确认要" + text + "该单位吗?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          modifyDisable(this.modifyqueryParams);
          this.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.forbidden = row.forbidden === "0" ? "1" : "0";
        });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const unitIds = row.unitId || this.ids;
      this.$confirm(
        '是否确认删除单位信息编号为"' + unitIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delUnit(unitIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有单位信息数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportUnit(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
    handleStatus() {
      this.getList();
    },
    handleDetail(row) {
      this.unitDetailRow = row;
      getUnit(row.unitId).then((res) => {
        this.formUnit = res.data;
        this.unitopen = true;
      });
      getGoingUnit({ unitId: row.unitId }).then((res) => {
        this.onGoingProject = res.rows;
        this.onGoingProject.forEach((item, index) => {
          item.index = index + 1;
        });
      });
    },
    getInfo(id) {
      getRandomresult({ extractId: id }).then((response) => {
        this.items = response.data;
        if (this.items.length < 2) {
          this.rowspanList == 5;
        } else {
          this.rowspanList = this.items.length + 4;
        }
      });
      getExtract(id).then((response) => {
        this.form = response.data;
        this.resultOpen = true;
        this.title = "";
      });
    },
    handleExtract(bool) {
      if(bool) {
        console.log(this.queryParams);
      }
    }
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-table .cell {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
::v-deep .el-table .cell:hover {
  white-space: normal;
}
.on-going-project {
  font-size: 18px;
  color: #303133;
  padding-left: 10px;
}
.going-project-table {
  padding: 20px 30px;
}
.clickable {
  cursor: pointer;
}
</style>
